local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_walrus_camp"] or {}
end
local function Player_Near_Check_Task(inst)
    inst:DoPeriodicTask(5,function()
        if inst:IsNearPlayer(20) and inst.fx == nil then
            local fx = inst:SpawnChild("npc_fx_snow_area")
            fx.Transform:SetPosition(0, 10, 0)
            inst.fx = fx
        elseif inst.fx then
            inst.fx:Remove()
            inst.fx = nil
        end

    end)    
end

local function walrus_spawn_task(inst)
    local function Spawn_task(inst)
        if inst.npc == nil or inst.npc:IsValid() ~= true then
            local pt = Vector3(inst.Transform:GetWorldPosition())
            local ret_pt = inst.npc_base_lib:GetSpawnPoint(inst,2.5)
            local npc = SpawnPrefab("monster_npc_walrus")
            npc:PushEvent("player_link",inst)
            npc.Transform:SetPosition(ret_pt.x, 0, ret_pt.z)
            npc:AddDebuff("npc_event_walrus_camp", "npc_event_walrus_camp")
            inst.npc = npc
            inst.SoundEmitter:PlaySound("dontstarve/common/pighouse_door")
        end
    end

    -------- onload 的时候
    inst:DoTaskInTime(1,function()
        if inst.npc_everything_data:Get("for_sale") ~= true then
            Spawn_task(inst)
        end
    end)
    -------- 每天任务
    if inst.npc_everything_data:Get("for_sale") ~= true then
        inst:WatchWorldState("cycles", function()
            inst:DoTaskInTime(10,function()
                if inst.npc_everything_data:Get("for_sale") ~= true then
                    Spawn_task(inst)
                end
            end)
        end)
    end

end



----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----- 添加和注册容器界面
local function container_Widget_change(theContainer)
    -----------------------------------------------------------------------------------
    ----- 容器界面名 --- 要独特一点，避免冲突
    local container_widget_name = "npc_item_walrus_camp_widget"

    -----------------------------------------------------------------------------------
    ----- 检查和注册新的容器界面
    local all_container_widgets = require("containers")
    local params = all_container_widgets.params
    if params[container_widget_name] == nil then
        params[container_widget_name] = {
            widget =
            {
                slotpos = {},
                animbank = "ui_fish_box_5x4",
                animbuild = "ui_fish_box_5x4",
                pos = Vector3(0, 220, 0),
                side_align_tip = 160,
            },
            type = "chest",
            acceptsstacks = true,                
        }

        for y = 2.5, -0.5, -1 do
            for x = -1, 3 do
                table.insert(params[container_widget_name].widget.slotpos, Vector3(75 * x - 75 * 2 + 75, 75 * y - 75 * 2 + 75, 0))
            end
        end
        ------------------------------------------------------------------------------------------
        ---- item test
        params[container_widget_name].itemtestfn =  function(container_com, item, slot)
            if item.components.perishable then
                return true
            end
            if item:HasOneOfTags({"fresh","stale","spoiled","saltbox_valid"}) then
                return true
            end
            return false
        end
        ------------------------------------------------------------------------------------------
    end

    theContainer:WidgetSetup(container_widget_name)
    ------------------------------------------------------------------------
end
local function add_container_before_not_ismastersim_return(inst)
    ------ 添加背包container组件    --- 必须在 SetPristine 之后，
    -- local container_WidgetSetup = "wobysmall"
    if TheWorld.ismastersim then
        inst:AddComponent("container")
        inst.components.container.openlimit = 1  ---- 限制1个人打开
        -- inst.components.container:WidgetSetup(container_WidgetSetup)
        container_Widget_change(inst.components.container)

    else
        inst.OnEntityReplicated = function(inst)
            container_Widget_change(inst.replica.container)
        end
    end
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddSoundEmitter()
    inst.entity:AddLight()
    inst.entity:AddNetwork()

    MakeObstaclePhysics(inst, 2)

    inst.AnimState:SetBank("walrus_house")
    inst.AnimState:SetBuild("walrus_house")
    inst.AnimState:PlayAnimation("idle")

    inst.MiniMapEntity:SetIcon("igloo.png")

    inst:AddTag("fridge")
    -- inst:AddTag("npc_item_sign_for_sale")
    inst:AddTag("structure")
    inst:AddTag("onbuilt_by_player")

    inst.Light:SetFalloff(1)
    inst.Light:SetIntensity(.5)
    inst.Light:SetRadius(2)
    inst.Light:SetColour(180/255, 195/255, 50/255)
    inst.Light:Enable(false)


    inst.entity:SetPristine()
    add_container_before_not_ismastersim_return(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")

    inst:AddComponent("inspectable") --可检查组件
    
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_walrus_camp")] = GetStringsTable().inspect_str  --人物检查的描述

    -------------------------------------------------------------------------------------
    ------ 玩家靠近灯光处理
        inst:AddComponent("playerprox")
        inst.components.playerprox:SetDist(5, 8)
        inst.components.playerprox:SetOnPlayerNear(function()
            inst.AnimState:PlayAnimation("lit")
            if TheWorld.state.isnight or TheWorld:HasTag("cave") then
                inst.Light:Enable(true) 
            end     
            inst.SoundEmitter:PlaySound("dontstarve/pig/pighut_lighton")  
        end)
        inst.components.playerprox:SetOnPlayerFar(function()
            inst.AnimState:PlayAnimation("idle")   
            inst.Light:Enable(false)
            inst.SoundEmitter:PlaySound("dontstarve/pig/pighut_lightoff")
        end)
    -------------------------------------------------------------------------------------
    ------ 冷源头
        inst:AddComponent("heater")
        inst.components.heater.heatfn = function() return -40 end
        inst.components.heater:SetThermics(false, true)
    -------------------------------------------------------------------------------------
    ---- 被拆卸
        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(20)
        inst.components.workable:SetOnFinishCallback(function(inst,worker)
            inst.components.container:DropEverything()
            SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition())  --- 烟雾
            inst:Remove()
        end)
        -- inst.components.workable:SetOnWorkCallback(function(inst,worker)        
        -- end)
    -------------------------------------------------------------------------------------
    ------ 物品保鲜
        inst:ListenForEvent("itemget",function(inst,_table)
            if _table and _table.item then
                local tempItem = _table.item
                if tempItem.components.perishable then
                    tempItem.components.perishable:StopPerishing()
                end
            end
        end)
        inst:ListenForEvent("itemlose",function(inst,_table)
            -- print("item lose")
            if _table and _table.prev_item then
                local tempItem = _table.prev_item
                if tempItem.components.perishable then
                    tempItem.components.perishable:StartPerishing()
                    -- tempItem.components.perishable:SetPercent(1)
                end
            end
        end)    
        inst:ListenForEvent("onclose",function()
            local ret = inst.components.container:FindItems(function(tempItem)
                if tempItem and tempItem.components.perishable then
                    tempItem.components.perishable:SetPercent(1)
                    return true
                end
            end)
        end)
    -------------------------------------------------------------------------------------
    ------ 玩家靠近才生成fx特效，减少卡顿
    Player_Near_Check_Task(inst)
    -------------------------------------------------------------------------------------
    walrus_spawn_task(inst)
    -------------------------------------------------------------------------------------
    ----- OnLoad 的时候检查是否出售
        inst:ListenForEvent("tag_for_sale_task",function()
            if inst.npc_everything_data:Get("for_sale") == true then
                inst.npc_base_lib:For_Sale_Sign_Show()
                inst.components.container.canbeopened = false
            end
        end)
        inst:DoTaskInTime(0,function()
            inst:PushEvent("tag_for_sale_task")
        end)
    -------------------------------------------------------------------------------------

    MakeHauntableWork(inst)    
    return inst
end

return Prefab("npc_item_walrus_camp", fn,assets),
    MakePlacer("npc_item_walrus_camp_placer", "walrus_house", "walrus_house", "idle")